import VueRouter from 'vue-router'
import Vue from 'vue'
Vue.use(VueRouter)

export let mainRouter = [
  {
    path: '/main/home',
    name: 'home',
    title:"首页",
    component: () => import(/* webpackChunkName: "about" */ '../views/main/home/index.vue'),
  },
  {
    path: '/main/classify',
    name: 'classify',
    title:"分类",
    component: () => import(/* webpackChunkName: "about" */ '../views/main/classify/index.vue'),
  },
  {
    path: '/main/shopcar',
    name: 'shopcar',
    title:"购物车",
    component: () => import(/* webpac、kChunkName: "about" */ '../views/main/shopcar/index.vue'),
  },
  {
    path: '/main/my',
    name: 'my',
    title:"我的",
    meta:{
      is_login:true,
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/main/my/index.vue'),
  }
]

const routes = [
  { 
    path: '/',
    redirect:"/main/home"
  },
  {
    path: '/login',
    name: 'login',
    component: ()=> import("../views/login/index.vue")
  },
  {
    path: '/main',
    name: 'main',
    component: () => import(/* webpackChunkName: "about" */ '../views/main/index.vue'),
    children:mainRouter
  },
  {
    path: '/details/:id',
    name: 'details',
    component: ()=> import(/* webpackChunkName: "details" */ '../views/details/index.vue'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) =>{
  console.log(to);
  if (to.meta.is_login && !window.localStorage.getItem("token")) {
      next("/login")
  }else{
    next()
  }
})

export default router
